package cn.maple.mq.transactionMessage;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class ProducerAffairMsg {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    /**
     * 发送事务消息
     */
    public SendResult sendTransMsg(){
        String msgBody="Producer发送事务消息";
        //封装消息
        Message<String> message = MessageBuilder.withPayload(msgBody).build();

        //发送事务消息
        return rocketMQTemplate.sendMessageInTransaction(
                //这里和事务监听器里面的事务组保持一致
                "Transaction-message-group",
                //topic:tag
                "queue_test_topic:message-tags",message ,null);
    }
}
